// src/services/authService.ts

import axios from 'axios'

import { User } from '../models/User';

const API_URL = 'http://localhost:8080/api';

export const login = async (email: string, password: string)  => {
    try {
        const response = await axios.post(`${API_URL}/auth/signin`, {
            email,
            password
        });

        // 返回完整的响应对象
        return {
            data: response.data, // 响应体数据
            headers: response.headers // 响应头
        };
    } catch (error){
        console.error('登录失败:', error);
        throw new Error('登录失败: 邮箱或密码错误');
    }
};

export const register = async (email:string,password:string,username:string) => {

    try {
        const response = await axios.post(`${API_URL}/auth/signup`, {
            email,
            password,
            username
        });
        // 返回完整的响应对象
        return {
            data: response.data, // 响应体数据
            headers: response.headers // 响应头
        };
    } catch (error) {
        console.error('登录失败:', error);
        throw new Error('登录失败: 邮箱或密码错误');
    }
};

export const logout = async (): Promise<void> => {
    await axios.post(`${API_URL}/auth/logout`, {});
};
